package org.rslai.autotest.core.driver.firefox;

import java.io.File;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

/**
 * FirefoxProfile 处理
 * @author rslai
 */
public class FirefoxProfile {
	private String profileDir;
	public void setProfileDir(String profileDir) {
		this.profileDir = profileDir;
	}

	private Properties pros;
	public void setPros(Properties pros) {
		this.pros = pros;
	}

	/**
	 * 设置FireFox配置信息
	 * @param fp FirefoxProfile
	 * @param key
	 * @param value
	 */
	private void setPreference(org.openqa.selenium.firefox.FirefoxProfile fp, String key, String value) {
		if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) {
			fp.setPreference(key, Boolean.valueOf(value).booleanValue());
		} else if (org.rslai.util.string.Verify.getInstance().isInteger(value)) {
			fp.setPreference(key, Integer.parseInt(value));
		} else {
			fp.setPreference(key, value);
		}
	}

	/**
	 * 返回 FirefoxProfile 信息
	 * @return FirefoxProfile
	 */
	public org.openqa.selenium.firefox.FirefoxProfile getFirefoxProfile() {
		org.openqa.selenium.firefox.FirefoxProfile fp;
		if (this.profileDir == null) {
			fp = new org.openqa.selenium.firefox.FirefoxProfile();
		} else {
			fp = new org.openqa.selenium.firefox.FirefoxProfile(new File(this.profileDir)); 
		}

		Iterator<Entry<Object, Object>> it = pros.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			this.setPreference(fp, (String) entry.getKey(), (String) entry.getValue());
		}

		return fp;
	}

}
